// const BASE_URL = 'http://172.16.3.255:8088'
const BASE_URL = 'http://1.116.236.51:7070'

const request = (obj) => {
	obj.url = obj.url || ''
	obj.method = obj.method || 'GET'
	obj.data = obj.data || {}
	obj.loading = obj.loading === false ? false : true; //是否显示请求加载中
	obj.requestTime = obj.requestTime || 500; //多久时间内完成网络请求，则不显示提示框
	let token = uni.getStorageSync('token')
	if(token){
		obj.header = {
			token:token
		}
	}

	let loadingStatus = true;
	//定义网络请求如果在一定时间内没有完成，则显示加载中提示框，默认时间为：500毫秒
	setTimeout(() => {
		if (loadingStatus && obj.loading) {
			uni.showLoading({
				title: "加载中",
				mask: true
			})
		}
	}, obj.requestTime)
	
	

	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + obj.url,
			method: obj.method,
			data: obj.data,
			header: obj.header,
			success: (res) => {
				resolve(res.data)
			},
			fail: (err) => {
				reject(err)
			},
			complete:() => {
				if(loadingStatus && obj.loading){
					uni.hideLoading()
				}
				loadingStatus = false
			}
		})
	})
}

export default request
